package com.zhiyou100.action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.entity.Rose;
import com.zhiyou100.service.RoseService;
import com.zhiyou100.util.object.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 角色 action
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-09-01 17:53
 */
@Controller
@RequestMapping("/rose")
public class RoseAction {
    @Autowired
    private RoseService service;


    @RequestMapping("/list.action")
    public String list(Model model,
                       @RequestParam(defaultValue = "1", value = "pageNo") Integer pageNo
    ) {
        // 每页显示的记录数
        int pageSize = 10;

        // 分页查询
        PageHelper.startPage(pageNo, pageSize);

        // 获取所有的信息
        final List<Rose> list = service.list();

        // 使用 PageInfo<Rose> 结果进行封装

        final PageInfo<Rose> pageInfo = new PageInfo<>(list);

        model.addAttribute("pageInfo", pageInfo);

        return "view/rose/list";
    }

    @RequestMapping("/getOne.action")
    public String getOne(Integer id, String method, Model model) {
        final Rose rose = service.selectOne(id);
        if (rose != null) {
            // 有，放入区域中，跳转 到 详情页面
            if (method == null || method.trim().length() == 0) {
                // 跳转到 view/rose/detail
                model.addAttribute("rose", rose);
                return "view/rose/detail";
            } else {
                // 跳转到更新界面
                model.addAttribute("rose", rose);
                return "view/rose/update";
            }

        } else {
            // 没有，返回列表
            return "view/rose/list";
        }
    }

    @RequestMapping("/insertOne.action")
    public String insertOne(Rose rose) {

        String id = "rid";
        if (ObjectUtil.onlyOneFiledIsNullAndTheFiledIsId(rose, id)) {
            // eid 字段为空，合法
            service.insertOne(rose);
        }

        return "/view/rose/list";
    }

    @RequestMapping("/updateOne.action")
    public String updateOne(Rose rose) {


        if (ObjectUtil.getNullFiled(rose).isEmpty()) {
            // 没有字段为 null，合法
            service.updateOne(rose);
        }

        return "/view/rose/list";
    }


    @RequestMapping("/deleteOne.action")
    public String deleteOne(Integer id) {
        if (id != null) {
            service.deleteOne(id);
        }
        return "view/rose/list";
    }
}
